﻿using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using UI;
using UnityEngine;

public enum EItemType
{
    None = 0,

    树的种子 = 1,
    七巧板 = 2,
    钥匙 = 3,
    手电筒 = 4,
    带锁日记本 = 5,
    魔方 = 6,
    柜子钥匙 = 7,
    有密码的存钱罐 = 8,
    零花钱 = 9,
    泡泡糖 = 10,
    贴纸 = 11,
    小人书 = 12,
    泡泡水 = 13,
    x音漫画 = 14,
    手机 = 15,
    物品清单 = 16,
    电脑 = 17,
    钥匙长大 = 18,
    带密码的盒子 = 19,
    游戏卡带 = 20,
    衣架 = 21,
    老家钥匙 = 22,
    不知名的树种 = 23,
    扫帚 = 24,
}

[DefaultExecutionOrder(-1)]
public class InventoryManager : MonoBehaviour
{
    //存放物品数据
    public ItemDataList_SO itemData;

    [SerializeField]
    public List<EItemType> ItemList => m_itemList;

    private static InventoryManager _instance;

    private List<EItemType> m_itemList;
    private HashSet<EItemType> m_itemSet;
    private HashSet<EItemType> m_gotItemSet;
    private HashSet<EItemType> m_usedItemSet;

    public static event Action<EItemType> OnGetItem;
    public static event Action<EItemType> OnUseItem;

    private void Awake()
    {
        _instance = this;
        m_itemList = new List<EItemType>();
        m_itemSet = new HashSet<EItemType>();
        m_gotItemSet = new HashSet<EItemType>();
        m_usedItemSet = new HashSet<EItemType>();
    }
    private void OnDestroy()
    {
        OnGetItem = null;
        OnUseItem = null;
    }
    private void AddItemInternal(EItemType itemType)
    {
        if (m_itemSet.Add(itemType))
        {
            m_gotItemSet.Add(itemType);
            m_itemList.Add(itemType);
            EventHandler.CallUpdateUIEvent(itemData.GetItemDetails(itemType), m_itemList.Count - 1);
            OnGetItem?.Invoke(itemType);
            Debug.Log($"获得道具: {itemType}");
            UI_TipPanel.ShowTip($"获得道具: {itemType.ToText()}");
        }
    }
    private void UseItemInternal(EItemType itemType)
    {
        if (m_itemSet.Remove(itemType))
        {
            m_usedItemSet.Add(itemType);
            m_itemList.Remove(itemType);
            if(m_itemList.Count == 0)
            {
                EventHandler.CallUpdateUIEvent(null, m_itemList.Count - 1);
            }
            else
            {
                EventHandler.CallUpdateUIEvent(itemData.GetItemDetails(m_itemList[m_itemList.Count - 1]), m_itemList.Count - 1);
            }
            
            OnUseItem?.Invoke(itemType);
            
            Debug.Log($"使用道具: {itemType}");
            UI_TipPanel.ShowTip($"使用了{itemType.ToText()}");
        }
    }

    public static void AddItem(EItemType itemType)
    {
        _instance.AddItemInternal(itemType);
    }
    
    public static void UseItem(EItemType itemType)
    {
        _instance.UseItemInternal(itemType);
    }
    public static bool HasItem(EItemType itemType)
    {
        return _instance.m_itemSet.Contains(itemType);
    }
    public static bool CheckItemGot(EItemType itemType)
    {
        return _instance.m_gotItemSet.Contains(itemType);
    }
    public static bool CheckItemUsed(EItemType itemType)
    {
        return _instance.m_usedItemSet.Contains(itemType);
    }

    public static List<EItemType> GetItem()
    {
        return _instance.m_itemList;
    }
}